
<!-- template - 登陆页面组件 -->

<template>
  <div id="poster">

    <!-- form - 登录信息填写 -->
    <el-form class="login-container" label-position="left" label-width="0px">
      <h3 class="login_title">系统登录</h3>
      <el-form-item>
        <el-input type='number' class="id" v-model="loginForm.id" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type='password' v-model="loginForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-radio-group style=" display:flex; justify-content: center; width: 100%" v-model="loginForm.identity">
          <el-radio label="student">学生</el-radio>
          <el-radio label="admin">管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <div style=" display :flex; justify-content: center; width: 100%">
          <!-- button - 选择登录或注册 -->
          <el-button type="primary" style="width: 30%;background: #505458;border: none" v-on:click="login">登录</el-button>
          <el-button type="primary" style="width: 30%;background: #505458;border: none" v-on:click="register">注册</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import store from '@/store'
import { ElMessage } from "element-plus";

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        id: null,
        password: null,
        identity: 'student'
      },
      responseResult: []
    }
  },
  created() {
    store.state.adminHead
  },
  methods: {
    // 登录按钮
    login() {
      console.debug('request = ', this.loginForm);

      //向后端发送post请求登录
      this.$http
        .post("/login", this.loginForm)
        .then(response => {
          console.debug('response = ', response)
          switch (response.data.errno){
            case 0: {this.UserInfoSubmit(); break;} // 登录成功
            case 12: {
              ElMessage({type: 'error', message: '用户名不存在或者密码错误'});
              break;
            }
            case 10: {
              ElMessage({type: 'error', message: '缺少必要参数'});
              break;
            }
          }
        })
    },
    // 注册按钮，跳转注册页面
    register() {
      this.$router.replace({ path: '/register' })
    },
    // 初始化登录用户信息
    UserInfoSubmit() {
      //会话存储该用户信息
      sessionStorage.setItem("userInfo", JSON.stringify(this.loginForm));
      //跳转主界面
      switch (this.loginForm.identity) {
        case 'student': {
          this.$router.replace({ path: '/student/projects' })
          break;
        }
        case 'admin': {
          this.$router.replace({ path: '/admin/manageUser' })
          break;
        }
      }
    },
  }
}
</script>

<style>
#poster {
  background: url("@/assets/logininterface.png") no-repeat center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}

body {
  margin: 0px;
}

.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin-top: 230px;
  margin-left: 250px;
  width: 300px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
